// pages/user/mycoupon.js
var app = getApp();
var rootDocment = app.globalData.postUrl;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hiddenLoading: false,
        userInfo: {},
        dataType: 1,
        dataList: [],
        shareItem: {},
        is_shop: -1,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    //下拉刷新
    onPullDownRefresh: function () {
        this.getList();
        wx.stopPullDownRefresh();
    },

    noUseBtn() {
        const is_shop = this.data.is_shop
        is_shop == 0.5 ? '榴莲会客厅' : is_shop == 2 ? '城市合伙人' : is_shop == 3 ? '分公司' : '价值合伙人'
        let content = is_shop == 0.5 ? '榴莲会客厅' : is_shop == 2 ? '城市合伙人' : is_shop == 3 ? '分公司' : '价值合伙人'
        wx.showModal({
            title: "提示",
            content: `您已是${content}级别，暂无法使用优惠券`,
            showCancel: false,
            confirmText: '关闭'
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function (opt) {
        var that = this

        that.setData({
            is_shop: wx.getStorageSync('is_shop')
        })

        //用户授权登录
        app.login(function (userInfo) {
            that.setData({
                userInfo: userInfo
            });
            that.getList();
        });
    },

    //切换
    changeType: function (e) {
        var that = this;
        that.setData({
            dataType: e.currentTarget.dataset.type,
            dataList: []
        });
        that.getList();
    },

    //获取列表
    getList: function () {
        var that = this;
        var paraArr = new Array();
        paraArr['state'] = that.data.dataType;
        paraArr['user_id'] = that.data.userInfo.userID;
        var sign = app.signature(paraArr);
        wx.request({
            url: rootDocment + '/api/coupon/index',
            data: {
                state: paraArr['state'],
                user_id: paraArr['user_id'],
                sign: sign
            },
            method: 'GET',
            header: {},
            success: function (res) {
                console.log(res.data);
                res.data.forEach(item => {
                    item._cut_price = Number(item.cut_price)
                })
                that.setData({
                    dataList: res.data,
                    hiddenLoading: true
                });
            }
        })
    },

    //点击优惠券
    hitCoupon: function (e) {
        var that = this;
        var id = e.currentTarget.dataset.id
        if (that.data.dataType == 1) { //去使用
            app.gotaburl('index/index');
        }
        if (that.data.dataType == 0) { //领取
            var paraArr = new Array();
            paraArr['m_id'] = id;
            paraArr['user_id'] = that.data.userInfo.userID;
            var sign = app.signature(paraArr);
            wx.request({
                url: rootDocment + '/api_coupon',
                data: {
                    m_id: id,
                    user_id: paraArr['user_id'],
                    sign: sign
                },
                method: 'POST',
                header: {},
                success: function (res) {
                    if (res.data.code == 1001) {
                        that.setData({
                            dataType: 0
                        });
                        wx.showToast({
                            title: '领取成功',
                            icon: 'none',
                            duration: 3000,
                            success: function () {
                                app.redirect('user/mycoupon');
                            }
                        })
                    } else {
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'none'
                        })
                    }
                }
            })
        }
    },

    shareBtn(e) {
        let {
            item
        } = e.currentTarget.dataset
        console.log(item)

        this.setData({
            shareItem: item
        })
    },

    onShareAppMessage(e) {
        if (e.from == 'button') {
            const item = e.target.dataset.item
            console.log(item)

            return {
                title: item.title,
                path: '/subpage/coupon_share/coupon_share?sid=' + item.id,
                imageUrl: 'https://lhs.tann.cc/upload/images/new/wechat_share_pic.jpg'
            }
        } else {
            return {
                title: '榴皇树',
                path: '/pages/index/index?scene=' + wx.getStorageSync('user_id3')
            }
        }
    }

})